package com.eduhzy.ttw.work.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.decoration.GridSpacingItemDecoration;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.work.R;
import com.eduhzy.ttw.work.R2;
import com.eduhzy.ttw.work.di.component.DaggerWorkUnSubmitComponent;
import com.eduhzy.ttw.work.di.module.WorkUnSubmitModule;
import com.eduhzy.ttw.work.mvp.contract.WorkUnSubmitContract;
import com.eduhzy.ttw.work.mvp.model.entity.WorkClassData;
import com.eduhzy.ttw.work.mvp.model.entity.WorkDetailData;
import com.eduhzy.ttw.work.mvp.presenter.WorkUnSubmitPresenter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.Subscriber;

import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-3 18:47:28
 */
@Route(path = RouterHub.WORK_WORKUNSUBMITFRAGMENT)
public class WorkUnSubmitFragment extends BaseFragment<WorkUnSubmitPresenter> implements WorkUnSubmitContract.View {
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    private HomeFragmentAdapter mFragmentAdapter;
    private WorkDetailData.HomeworkClassListBean mBean;
    @Inject
    BaseQuickAdapter<WorkClassData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    private String title;

    public static WorkUnSubmitFragment newInstance(WorkDetailData.HomeworkClassListBean bean, int type) {
        WorkUnSubmitFragment fragment = new WorkUnSubmitFragment();
        WorkDetailData.HomeworkClassListBean newBean = null;
        try {
            newBean = (WorkDetailData.HomeworkClassListBean) bean.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        newBean.setType(type);
        fragment.setData(newBean);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerWorkUnSubmitComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .workUnSubmitModule(new WorkUnSubmitModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.work_fragment_work_un_submit, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        getData();
        mRvList.setLayoutManager(mLayoutManager);
        mRvList.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(() -> getData());
        GridSpacingItemDecoration decoration = new GridSpacingItemDecoration(4, RxUtil.getAutoHeight(getActivity(), 30), true);
        mRvList.addItemDecoration(decoration);
    }

    private void getData() {
        mPresenter.workRecordList4Teacher(mBean.getWorkId(), mBean.getClassId(), mBean.getType());
    }

    @Subscriber(tag = RouterHub.WORK_WORKUNSUBMITFRAGMENT)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.CHECK_COMPLETE:
                getData();
                break;
            default:
        }
    }

    @Override
    public void setData(@Nullable Object data) {
        this.mBean = (WorkDetailData.HomeworkClassListBean) data;
    }


    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    public void setPageAdapter(HomeFragmentAdapter adapter) {
        this.mFragmentAdapter = adapter;
    }

    @Override
    public void updatePagerTitle(int count) {
        if (mFragmentAdapter != null) {
            for (int i = 0; i < mFragmentAdapter.getCount(); i++) {
                if (mFragmentAdapter.getItem(i) == WorkUnSubmitFragment.this) {
                    if (title == null) {
                        title = mFragmentAdapter.getPageTitle(i).toString();
                    }
                    mFragmentAdapter.setPageTitle(i, String.format(Locale.CHINESE, "%s  %s", title, count));
                    break;
                }
            }
        }
    }
}
